/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package in.jquickfinder.models;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

/**
 *
 * @author Administrator
 */
public class TablePopupListener extends MouseAdapter {

    private JPopupMenu popupMenu;
    private JTable table;

    public TablePopupListener(JPopupMenu popupMenu, JTable table) {
        this.popupMenu = popupMenu;
        this.table = table;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        firePopup(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        firePopup(e);
    }

    private void firePopup(MouseEvent e) {
        if (e.isPopupTrigger() && table.getModel().getRowCount() != 0
                && table.getSelectedRow() != -1) {
            popupMenu.show(table, e.getX(), e.getY());
        }
    }
}
